<template>
    <div class="auth">
        <input type="button" value="点击登录" @click="startAuth">
    </div>
</template>

<script>
export default {
    name:'auth',
    data(){
        return {

        }
    },
    methods:{
        startAuth(){
            let token = localStorage.getItem('token');
            alert('start auth'+token);
            let that = this;
            let currentUrl = 'http://dahua-soft.com/frontend/#/auth';
            //window.location.href = 'http://172.16.0.97:8080/auth?code=100'
            that.$ajax(that, 'post', that.$api.userCode, {'url': currentUrl}, null, function(res){
                if(res.data['error_code'] == 0){
                    // alert(JSON.stringify(res));
                    window.location.href = res.data.msg;
                }else{
                    alert(JSON.stringify(res));
                }
            })
        },
        getToken(){
            let testCode = /code=/;
            let that = this;
            let currentUrl = window.location.href;
            // alert('home page get appid'+testCode.test(currentUrl));
            if(testCode.test(currentUrl)){
                let code = that.getUrlCode(currentUrl);
                let disId = localStorage.getItem('distribution');
                alert(code);
                alert(localStorage.getItem('token'));
                // that.$router.push('/container/home?code='+code);
                that.$ajax(that, 'post', that.$api.userJudge, {'code':code, 'distributor_id':disId}, null, function(res){
                    alert(JSON.stringify(res.data.msg));
                    if(res.data['error_code'] == 0){
                        alert('userJudge success===='+JSON.stringify(res));
                        let temRes = res.data.msg;
                        localStorage.setItem('token', temRes.token);
                        that.$router.push('/container/home?code='+code);
                    }else{
                        alert('userJudge error==='+JSON.stringify(res));
                    }
                })
            }
        }
    },
    mounted(){
        this.$store.state.tab = '登录';
    },
    activated(){
        this.$store.state.tab = '登录';
        this.getToken();
    }
}
</script>

<style lang='less'>
@rem:100rem;

.auth{
    width: 100%;
    margin-top: 88/@rem;
    padding-top: 100/@rem;
}
</style>
